import axios from 'axios';

import { ElMessage } from 'element-plus'

import useUserStore  from '@/store/modules/user';

const request = axios.create({
      baseURL: '/api',//请求的基础路径的设置
      timeout: 5000//超时的时间的设置,超出五秒请求就是失败的
})

request.interceptors.request.use(function (config) {
      let userStore = useUserStore();
      if (userStore.userInfo) {
            config.headers.token = userStore.userInfo.token
      }
      return config;
}, function (error) {
      return Promise.reject(error);
});

request.interceptors.response.use(function (response) {
      return response.data;
}, function (error) {
      let status = error.response.status;
      switch (status) {
            case 404:
                  //错误提示信息
                  ElMessage({
                        type: 'error',
                        message: '请求失败路径出现问题'
                  })
                  break;
            case 500 | 501 | 502 | 503 | 504 | 505:
                  ElMessage({
                        type: 'error',
                        message: '服务器挂了'
                  })
                  break;
            case 401:
                  ElMessage({
                        type: 'error',
                        message: '参数有误'
                  })
                  break;
      }
      return Promise.reject(error.message);
});


export default request